﻿using System;
using System.Reflection;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;

namespace MediaMover.Data.NHIbernate
{
    public class Helper
    {
        private static ISessionFactory _sessionFactory;
        public static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                {
                    _sessionFactory = CreateSessionFactory();
                }
                return _sessionFactory;
            }
            set { _sessionFactory = value; }
        }

        static ISessionFactory CreateSessionFactory()
        {
            string connectionString = Environment.MachineName;
            var config = Fluently.Configure()
                .Database(MsSqlCeConfiguration.Standard.ConnectionString("Data Source=MediaMoverDatabbase.sdf"))
                .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()).Conventions.Add(FluentNHibernate.Conventions.Helpers.LazyLoad.Never()))
                .BuildSessionFactory();
            return config;
                
        }

        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }
    }
}
